package com.cn.cxq.bean;


import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;

import com.cn.cxq.util.SQLUtil;

public abstract class BasicBean {

	public DataSource dataSource;

	public JdbcTemplate jdbcTemplate;

	@Autowired
	public void init(DataSource dataSource) {
		this.dataSource = dataSource;

		this.jdbcTemplate = new JdbcTemplate(this.dataSource);
	}

	public Timestamp getTime() throws SQLException {
		Connection con = null;
		try {
			con = getConnection();
			return SQLUtil.getTime(con);
		} finally {
			releaseConnection(con);
		}
	}

	public Connection getConnection() throws SQLException {
		return DataSourceUtils.getConnection(this.dataSource);
	}

	public void releaseConnection(Connection con) throws SQLException {
		DataSourceUtils.releaseConnection(con, this.dataSource);
	}

}
